#include <workflow/WFFacilities.h>
#include <workflow/WFTaskFactory.h>
#include <workflow/HttpUtil.h>
#include <workflow/Workflow.h>
#include <workflow/WFHttpServer.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <vector>
#include <iostream>

using std::string;
using std::vector;

struct SeriesContext {
    string password;
    protocol::HttpResponse *serverResp;
};

static WFFacilities::WaitGroup waitGroup(1);

void handler(int signum) {
    printf("done!\n");
    waitGroup.done();
}

void redisCallback(WFRedisTask *redisTask) {
    protocol::RedisValue value;
    redisTask->get_resp()->get_result(value); // 用户在redis数据库当中的密码
    SeriesContext *context = static_cast<SeriesContext *>(series_of(redisTask)->get_context());
    //context->password 是 用户传入的密码
    if (value.is_ok() && value.is_string() && value.string_value() == context->password) {
        context->serverResp->append_output_body("<html>OK</html>");
    } else {
        context->serverResp->append_output_body("<html>NO</html>");
    }
}

void process(WFHttpTask *serverTask) {
    protocol::HttpRequest *req = serverTask->get_req();
    string method = req->get_method();
    string uri = req->get_request_uri(); // /login?user=chizuru&password=123456
    protocol::HttpResponse *resp = serverTask->get_resp();

    printf("%s\n", uri.c_str());
    if (method == "GET") {
        // 提取用户信息
        string nameKV = uri.substr(0, uri.find("&")); // /login?user=niqiu
        string passwordKV = uri.substr(uri.find("&") + 1); // password=123
        string name = nameKV.substr(nameKV.find("=") + 1);
        string password = passwordKV.substr(passwordKV.find("=") + 1);

        // 创建一个redis任务
        WFRedisTask *redisTask = WFTaskFactory::create_redis_task(
                "*",
                10,
                [password, serverTask](WFRedisTask *redisTask) {
                    protocol::RedisValue value;
                    redisTask->get_resp()->get_result(value);

                    if (value.string_value() == password) {
                        serverTask->get_resp()->append_output_body("<html>ok</html>");
                    } else {
                        serverTask->get_resp()->append_output_body("<html>false</html>");
                    }
                }
        );

        redisTask->get_req()->set_request("get", {name});
        series_of(serverTask)->push_back(redisTask);

#ifdef do_not_use_lambda
        // 准备context 将会共享给redis任务
        SeriesContext *context = new SeriesContext;
        context->password = password;
        context->serverResp = resp;
        series_of(serverTask)->set_context(context);
#endif
    }

}

int main() {
    signal(SIGINT, handler);
    /*创建一个server对象*/
    WFHttpServer server(process);

    /**
     * 启动server，之后server会等待客户端接入
     * 当有客户端接入时，创建一个序列，执行任务，任务的基本工作是process
     */
    if (server.start(8889) == 0) {
        waitGroup.wait();
        server.stop();
    } else {
        perror("server start failed");
        return -1;
    }

    return 0;
}